package cn.alcatraz.org.service.impl;

import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import cn.alcatraz.basic.utils.MD5Utils;
import cn.alcatraz.basic.utils.PageList;
import cn.alcatraz.basic.utils.StrUtils;
import cn.alcatraz.org.domain.Employee;
import cn.alcatraz.org.mapper.EmployeeMapper;
import cn.alcatraz.org.query.EmployeeQuery;
import cn.alcatraz.org.service.IEmployeeService;
import cn.alcatraz.user.domain.Logininfo;
import cn.alcatraz.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void save(Employee employee) {
        //初始化emplpyee并保存
        initEmployee(employee);
        employeeMapper.save(employee);
        //初始化logininfo并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        //将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());

    }

    /**
     * @Title: initRelationTable
     * @Description: 初始化关联表
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/13 11:42
     * @Parameters: [role]
     * @Return void
     */
    public void initRelationTable(Employee employee){
        //根据roleId和菜单Id和权限Id进行新增
        if (employee.getRoles() != null){
            //根据roleId删除角色菜单和角色权限关联表对应数据
            employeeMapper.deleteEmployeeRole(employee.getId());
            employeeMapper.saveEmployeeRole(employee.getId(), employee.getRoles());
        }
    }

    /**
     * @Title: initEmployee
     * @Description: 初始化employee
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/13 11:43  
     * @Parameters: [employee]
     * @Return void
     */
    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }

    /**
     * @Title: employee2Logininfo
     * @Description: 将employee的数据拷贝到logininfo中
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/13 11:44  
     * @Parameters: [employee]
     * @Return cn.alcatraz.user.domain.Logininfo
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }



    @Override
    public void update(Employee employee) {
        super.update(employee);
        //初始化logininfo并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
        //将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
    }


    @Override
    public Employee loadByLogininfoId(Long logininfoId) {
        return employeeMapper.loadByLogininfoId(logininfoId);
    }
}
